package com.root.dreameraadmin.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.root.dreameraadmin.R;
import com.root.dreameraadmin.utils.HttpConnectionUtil;

import java.util.HashMap;

/**
 * Created by root on 16-6-27.
 */
public class AddpointActivity extends Activity {
    private static final String TAG="AddPointActivity";

    private EditText point_name;
    private EditText location;
    private Button submit;
    private Button clear;

    HashMap<String,Object> paramMap;

    private String input_name=null;
    private double input_longitude=new Double(0.0);
    private double input_latitude=new Double(0.0);

    //百度地图控件
    private MapView mMapView = null;
    // 百度地图对象
    private BaiduMap bdMap;
    private BMapManager manager;
    private LatLng poi_latLng=new LatLng(0.0,0.0);

    private boolean flag = false;

    public static RelativeLayout map;//地图的容器


    private LocationManager locationManager;
    private String provider;

    public AddpointActivity() {
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.point_add_view);
        init();


        manager = new BMapManager();
        manager.init();
        point_name = (EditText) findViewById(R.id.inputName);
//        location = (EditText) findViewById(R.id.Latlng);
        submit = (Button) findViewById(R.id.submit);
        clear=(Button)findViewById(R.id.clear);
    }

    private void init() {
        mMapView = (MapView) findViewById(R.id.id_bmapView);
//        map = (RelativeLayout)findViewById(R.id.id_map);
        bdMap = mMapView.getMap();
        LatLng latLng = new LatLng(41.802273,123.417315);//将地图移至沈阳市
        MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
        bdMap.animateMapStatus(u);
    }

    @Override
    protected void onStart(){
        super.onStart();
        Log.d(TAG,"onStart");
    }
    @Override
    protected void onResume(){
        super.onResume();
        Log.d(TAG,"onResume");

        mMapView.onResume();
        submit.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Log.d(TAG,"submit data");
                if(point_name.getText().toString().equals("")||location.getText().toString().equals("")){
                    if(point_name.getText().toString().equals("")&&!location.getText().toString().equals(""))
                             Toast.makeText(AddpointActivity.this, "the point name can not be null!", Toast.LENGTH_SHORT).show();
                    else if(!point_name.getText().toString().equals("")&&location.getText().toString().equals(""))
                             Toast.makeText(AddpointActivity.this, "the location can not be null!", Toast.LENGTH_SHORT).show();
                     else  Toast.makeText(AddpointActivity.this, "the point name and location can not be null!", Toast.LENGTH_SHORT).show();
                }
                else{
                input_name = point_name.getText().toString();// get input data
                //input_longitude = Float.parseFloat(point_longitude.getText().toString());
                //input_latitude = Float.parseFloat(point_latitude.getText().toString());

                paramMap = new HashMap<String, Object>();
                paramMap.put("name", input_name);
                paramMap.put("longitude", input_longitude);
                paramMap.put("latitude", input_latitude);
                paramMap.put("city","");

                new AlertDialog.Builder(AddpointActivity.this).setTitle("系统提示")//设置对话框标题
                        .setMessage("ADD this point?")//设置显示的内容
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {//添加确定按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                //new thread to add
                                new Thread() {
                                    public void run() {
                                        try{
                                            HttpConnectionUtil.doPost(HttpConnectionUtil.PLACEURL, paramMap);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    if (HttpConnectionUtil.result == "success") {
                                                        Toast.makeText(AddpointActivity.this, "添加成功!", Toast.LENGTH_SHORT).show();
                                                        point_name.setText("");
                                                        location.setText("");
                                                        paramMap.clear();
                                                    }
                                                    else{
                                                        Toast.makeText(AddpointActivity.this, "添加失败!", Toast.LENGTH_SHORT).show();
                                                        paramMap.clear();
                                                    }
                                                }
                                            });
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                }.start();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {//添加返回按钮
                            @Override
                            public void onClick(DialogInterface dialog, int which) {//响应事件
                                // TODO Auto-generated method stub
                                dialog.dismiss();
                            }
                        }).show();//在按键响应事件中显示此对话框
                }

            }//end else
        });
        clear.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                point_name.setText("");
                location.setText("");
            }
        });

        bdMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {

                Log.d(TAG,"omMapClick");
                System.out.print("latitude+"+latLng.latitude+","+"longitude+"+latLng.longitude);
                input_longitude=latLng.longitude;
                input_latitude=latLng.latitude;
                location.setText(latLng.toString());

            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
    }
    @Override
    protected void onPause(){
        super.onPause();
        Log.d(TAG,"onPause");
        mMapView.onPause();
    }
    @Override
    protected void onStop(){
        super.onStop();
        Log.d(TAG,"onStop");
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        Log.d(TAG,"onDestroy");
        mMapView.onDestroy();
    }
    @Override
    protected void onRestart(){
        super.onRestart();
        Log.d(TAG,"onRestart");
    }

}
